//
//  StoresListViewController.m
//  BMProject
//
//  Created by chris on 16/1/2.
//  Copyright © 2016年 BMProject. All rights reserved.
//

#import "StoresListViewController.h"
#import "StoresTopView.h"
#import "StoresCell.h"
#import "cardChooseView.h"
#import "DataModel.h"
#import "StoresInformationViewController.h"
@interface StoresListViewController ()<UITableViewDataSource,UITableViewDelegate,cardChooseViewDelegate>
{
    cardChooseView *cardChooseView1;
    SelectTypeBean *selectCardBean;
    StoresTopView *topView;
    UITableView *tableview;
    NSMutableArray *_tableDataArr;//StoresListModel 类型

}
@end

@implementation StoresListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"门店信息";
    _tableDataArr = [[NSMutableArray alloc] init];
    UIView * bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 140)];
    bgView.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1];
    
    topView = [[[NSBundle mainBundle] loadNibNamed:@"StoresTopView" owner:self options:nil]firstObject];
    topView.frame = CGRectMake(0, 0, DEF_SCREEN_WIDTH, 130);
    topView.statusTF.placeholder = @"--请选择--";
    cardChooseView1 = [[[NSBundle mainBundle] loadNibNamed:@"cardChooseView" owner:nil options:nil] lastObject];
    cardChooseView1.delegate = self;
    NSMutableArray *cardList = [NSMutableArray array];
    [cardList addObjectsFromArray:@[[SelectTypeBean beanWithName:@"待审核"],
                                    [SelectTypeBean beanWithName:@"渠道经理审核通过"],
                                    [SelectTypeBean beanWithName:@"渠道经理审核驳回"],
                                    [SelectTypeBean beanWithName:@"分公司审核通过"],
                                    [SelectTypeBean beanWithName:@"分公司审核驳回"],
                                    ]];
    cardChooseView1.cardList = cardList;
    topView.statusTF.inputView = cardChooseView1;
    [topView.searchBtn addTarget:self action:@selector(searchAction:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:topView];

    tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT-64) style:UITableViewStyleGrouped];
    tableview.delegate = self;
    tableview.dataSource = self;
    tableview.sectionFooterHeight = 10;
    tableview.sectionHeaderHeight = 10;
    tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableview.tableHeaderView = bgView;
    tableview.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        super.page = 1;
        [self requestAuditListWithPage:kIntToString(super.page)];
    }];
    tableview.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        super.page++;
        [self requestAuditListWithPage:kIntToString(super.page)];
    }];
    [self addSubview:tableview];
    [tableview.header beginRefreshing];
}
#pragma mark-UITableViewDateSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _tableDataArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellId = @"StoresCell";
    StoresCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell)
    {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"StoresCell" owner:self options:nil]lastObject];
    }
    [cell setVc:self];
    [cell setInfo:_tableDataArr[indexPath.section]];
    return cell;
}
#pragma mark- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 130;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    StoresListModel *model = [_tableDataArr objectAtIndex:indexPath.section];
    StoresInformationViewController * information = [[StoresInformationViewController alloc]init];
    information.model = model;
    [self.navigationController pushViewController:information animated:YES];
}

#pragma mark-cardChooseViewDelegate
- (void)cancel {
    [self.view endEditing:YES];
}

- (void)chooseCard:(SelectTypeBean *)cardInfo {
    selectCardBean = cardInfo;
    topView.statusTF.text = cardInfo.name;
    [self.view endEditing:YES];
}

- (void)cardChooseView:(UIPickerView *)cardChooseView didSelectRow:(NSInteger)row withCardBean:(SelectTypeBean *)cardInfo {
    selectCardBean = cardInfo;
    topView.statusTF.text = cardInfo.name;
}

#pragma mark-event
- (void)searchAction:(UIButton *)searchButton
{
    DLog(@"搜索事件--");
    [self requestAuditListWithPage:@"1"];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)requestAuditListWithPage:(NSString *)page {
        NSString *sign;
        if([topView.statusTF.text isEqualToString:@"待审核"]){
            sign = @"1";
        }else if([topView.statusTF.text isEqualToString:@"渠道经理审核通过"]) {
            sign = @"2";
        }else if([topView.statusTF.text isEqualToString:@"渠道经理审核驳回"]) {
            sign = @"3";
        }else if([topView.statusTF.text isEqualToString:@"分公司审核通过"]) {
            sign = @"4";
        }else if([topView.statusTF.text isEqualToString:@"分公司审核驳回"]) {
            sign = @"5";
        }else{
            sign = @"";
        }
        [UserServices getAuditListByToken:[[AppDelegate appDelegate] getUserInfo].token
                      pageSize:kIntToString(kListPageSize)
                          page:kIntToString(super.page)
                          sign:sign
                          name:topView.nameTF.text
                      servicer:topView.numTF.text
               completionBlock:^(int result, id responseObject) {
                   DLog(@"order = %@",responseObject);
                   if (result == API_Request_Success) {
                       NSArray *array = responseObject;

                       NSMutableArray *list = [NSMutableArray array];
                       [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                           StoresListModel *model = [[StoresListModel alloc] initWithDictionary:obj error:nil];
                           [list addObject:model];
                       }];


                       if (super.page == 1) {
                           [_tableDataArr removeAllObjects];
                           [_tableDataArr addObjectsFromArray:list];
                           [tableview.footer resetNoMoreData];//重置有更多数据
                           if (_tableDataArr == nil || _tableDataArr.count == 0) {
                               //提示无数据
                               [SVProgressHUD showErrorWithStatus:@"查无数据"];
                           } else {
                           }
                       }
                       else
                           [_tableDataArr addObjectsFromArray:list];
                       if (list.count == 0) {
                           super.page--;
                       }
                       [tableview.header endRefreshing];
                       [tableview.footer endRefreshing];
                       [tableview reloadData];
                       if (list.count < kListPageSize)
                           [tableview.footer noticeNoMoreData];//没有更多数据
                   } else {
                       [tableview.header endRefreshing];
                       [tableview.footer endRefreshing];
                   }
               }];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
